/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ZeroHora.DAO;
import br.com.cadastros.bean.Usuario;
import br.com.cadastros.bean.Usuario;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Gledson
 */
public class usuarioDAO extends DAO{
    public void cadastrar(Usuario u) throws Exception{
        openDatabase();
            String sql = "INSERT INTO usuario VALUES (null,?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, u.getNome());
            pstmt.setString(2, u.getTelefone());
            pstmt.setString(3, u.getEmail());
            pstmt.setString(4, u.getSenha());
            pstmt.execute();
        closeDatabase();
    }
 public Usuario busca(Integer id) throws Exception{
        openDatabase();
        String sql = "select * from usuario where idUsuario=?";
        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        Usuario u = new Usuario();
        if(rs.next()){
            u.setIdUsuario(rs.getInt("idCliente"));
            u.setNome(rs.getString("nome"));
            u.setTelefone(rs.getString("telefone"));           
            u.setEmail(rs.getString("email"));
            u.setSenha(rs.getString("senha"));
        }
        return u;
    }
    
    public void delete(Usuario u)throws Exception{
        openDatabase();
        String sql = "DELETE from usuario WHERE idUsuario = ?";
        pstmt = con.prepareStatement(sql);//Não é obrigatório, ele prepara o sql
        pstmt.setInt(1, u.getIdUsuario());
        pstmt.execute();
    }
    
    public List<Usuario> listaUsuario() throws Exception{
        openDatabase();
        String sql = "SELECT * FROM ususario";
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
        Usuario u = null;
       List<Usuario> listaUsuario = new ArrayList<Usuario>();
       while(rs.next()){
           u = new Usuario();
                 u.setNome(rs.getString("nome"));
                 u.setTelefone(rs.getString("telefone"));
                 u.setEmail(rs.getString("email"));
                 u.setSenha(rs.getString("senha"));

           listaUsuario.add(u);
       }           
       return listaUsuario;        
    }
    
    public void update(Usuario u)throws Exception{
        openDatabase();
        String sql = "UPDATE produto SET nome=?, telefone=?,  email=?, senha=?" + 
                "WHERE idUsuario=?";
       pstmt = con.prepareStatement(sql);
       pstmt.setString(1, u.getNome());
       pstmt.setString(2, u.getTelefone());        
       pstmt.setString(3, u.getEmail());        
       pstmt.setString(4, u.getSenha());        
       pstmt.setInt(5, u.getIdUsuario());     
       pstmt.executeUpdate();
        
    }
}

    

